package com.sh.personnel.controller.sys;

import com.sh.personnel.domain.ITbDepartment;
import com.sh.personnel.service.IDeptService;
import com.sh.personnel.util.result.Result;
import com.sh.personnel.vo.dto.SelectTreeDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门控制
 * @author 林思浩
 * @date 2020/03/12 17:51
 */
@RestController
@RequestMapping("/dept")
public class IDeptController {

    @Autowired
    IDeptService deptService;

    //获取所有部门树形集合
    @GetMapping("/tree")
    public Result getDeptAllTree() {
        List<SelectTreeDto> list = deptService.getDepartmentTree();
        return Result.success().setData("dept_tree", list);
    }

    //添加部门
    @PostMapping()
    @PreAuthorize("hasAuthority('sys:dept:insert')")
    public Result addDepartment(@RequestBody ITbDepartment department) {
        boolean bool = deptService.addDepartment(department);
        return bool ? Result.success() : Result.fail();
    }

    //更新部门
    @PutMapping()
    @PreAuthorize("hasAuthority('sys:dept:update')")
    public Result alterDepartment(@RequestBody ITbDepartment department) {
        boolean bool = deptService.updateDepartment(department);
        return bool ? Result.success() : Result.fail();
    }

    //获取部门信息
    @GetMapping("/{id}")
    @PreAuthorize("hasAuthority('sys:dept:info')")
    public Result getDepartmentById(@PathVariable String id) {
        ITbDepartment department = deptService.getDepartmentById(id);
        return Result.success().setData("department", department);
    }

    //删除部门
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('sys:dept:delete')")
    public Result deleteDepartment(@PathVariable String id) {
        boolean bool = deptService.deleteDepartmentById(id);
        return bool ? Result.success() : Result.fail();
    }
}
